﻿using C556Backside.Commands.Voluntary;
using C556Backside.Models;
using C556Backside.ReturnModels;
using C556Backside.Services;
using C556Backside.ViewModel.Voluntary;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace C556Backside.Controllers;

[ApiController]
[Route("SelectIdentity/Voluntary")]
[EnableCors("CorsPolicy")]
public class VoluntaryController
{
    public C556DBContext DBContext { get; set; }
    public VoluntaryController(C556DBContext c556DBContext)
    {
        DBContext = c556DBContext;
    }

    [HttpPost]
    [Route("Login")]
    public ReturnModel Login([FromBody] CVoluntaryLogin voluntaryLogin)
    {
        if (voluntaryLogin == null)
        {
            return new ErrorReturn { ErrorMessage = "未提交数据" };
        }
        try
        {
            var result = DBContext.Voluntary.SingleOrDefault(o => o.VoluntaryName == voluntaryLogin.UserName && o.VoluntaryPassword == voluntaryLogin.Password);
            if (result != null)
            {
                return new LoginReturn<VoluntaryModel>(result) { Identity = LoginReturn<VoluntaryModel>.LoginIdentity.Voluntary };
            }
            return new ErrorReturn { ErrorMessage = "未找到这位志愿人员，请确认账号密码" };
        }
        catch
        {
            return new ErrorReturn { ErrorMessage = "出现异常，请联系系统管理人员" };
        }
    }
    [HttpPost]
    [Route("SignIn")]
    public ReturnModel SignIn([FromBody] CVoluntarySignIn signIn)
    {
        if (signIn == null)
        {
            return new ErrorReturn { ErrorMessage = "未提交数据，请重试" };
        }
        try
        {
            var newVoluntary = new VoluntaryModel
            {
                VoluntaryName = signIn.UserName,
                VoluntaryPassword = signIn.Password,
                VoluntaryTelephone = signIn.Telephone,
                Province = signIn.Province,
                City = signIn.City,
                Area = signIn.Area
            };
            var result = DBContext.Voluntary.Add(newVoluntary).Entity;
            DBContext.SaveChanges();
            return new SignInReturn<VoluntaryModel>(result) { Identity = SignInReturn<VoluntaryModel>.SignInIdentity.Voluntary };
        }
        catch
        {
            return new ErrorReturn { ErrorMessage = "出现异常，请联系管理人员" };
        }
    }
    [HttpPost]
    [Route("JoinVoluntaryActivity")]
    public bool JoinVoluntaryActivity([FromBody] CJoinVoluntaryActivity join)
    {
        try
        {
            var existVr = DBContext.VAVRelationshipsTable.FirstOrDefault(o => o.ActivityID == join.ActivityID && o.VoluntaryID == join.VoluntaryID);
            if (existVr == null)
            {
                var vrAndV = new VAandVRelationshipsModel { ActivityID = join.ActivityID, VoluntaryID = join.VoluntaryID,JoinTime=DateTime.Now.ToLocalTime() };
                DBContext.VAVRelationshipsTable.Add(vrAndV);
                return DBContext.SaveChanges() > 0;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
    [HttpPost]
    [Route("WriteReview")]
    public bool WriteReview([FromBody] CVoluntaryWriteReview review)
    {
        var dbReview = new ActivityReviewModel { VAAndVID=review.VAandVID, ReviewContent = review.ReviewContent };
        DBContext.ActivityReviews.Add(dbReview);
        return DBContext.SaveChanges() > 0;
    }
    [HttpGet]
    [Route("GetMyVoluntaryByVoluntaryID")]
    public List<VGetMyVoluntaryByVoluntaryIDModel> GetMyVoluntaryByVoluntaryID([FromQuery] CGetMyVoluntaryByVoluntaryIDModel model)
    {
        var list= new List<VGetMyVoluntaryByVoluntaryIDModel>();
        foreach(var item in DBContext.VAVRelationshipsTable.Include(o => o.VoluntaryActivityModel).ThenInclude(o=>o.ActivityTypeModel).Where(o=>o.VoluntaryID==model.VoluntaryID).OrderByDescending(e=>e.JoinTime))
        {
            var vModel=new VGetMyVoluntaryByVoluntaryIDModel();
            vModel.ActivityID = item.ActivityID;
            vModel.RelationID = item.RelationID;
            vModel.ActivityName=item.VoluntaryActivityModel.ActivityName;
            vModel.ActivityJoinTime = item.JoinTime.ToLocalTime().ToString();
            vModel.ActivityTypeName = item.VoluntaryActivityModel.ActivityTypeModel.ActivityTypeName;
            vModel.IsComplete = item.VoluntaryActivityModel.ActivityComplete;
            list.Add(vModel);
        }
        return list;
    }
}
